package service

import (
	"goadmin/models"
	"gorm.io/gorm"
)

type ProductService struct{ db *gorm.DB }

// List 分页获取在售商品列表
func ProductList(page, pageSize int) (total int64, list []models.Product, err error) {
	query := models.DB.Model(&models.Product{}).Where("status = ?", 1)
	if err = query.Count(&total).Error; err != nil {
		return
	}
	offset := (page - 1) * pageSize
	err = query.Limit(pageSize).Offset(offset).Order("id desc").Find(&list).Error
	return total, list, err
}

// Get 根据 ID 获取商品详情
func GetProduct(id uint64) (p models.Product, err error) {
	err = models.DB.Model(&models.Product{}).First(&p, id).Error
	return p, err
}
